package com.zyh.em.update;

import com.zyh.em.entity.HistoryData;
import com.zyh.em.entity.LoginSuccessData;

import java.util.ArrayList;
import java.util.List;

public class DeviceUpdate extends AbstractUpdate{
    private int threshold;

    public DeviceUpdate(int threshold){
        this.threshold = threshold;
    }
    /**
     * 更新方法
     *
     * @param loginSuccessData 登录成功数据
     * @param historyData      历史数据
     * @param updateChain      更新链
     */
    @Override
    public void update(LoginSuccessData loginSuccessData, HistoryData historyData, UpdateChain updateChain) {
        List<String> devices = historyData.getDevices();
        if (devices==null){
            devices = new ArrayList<>();
            historyData.setDevices(devices);
        }

        devices.add(loginSuccessData.getDevice());
        if (devices.size()>threshold){
            devices.remove(0);
        }
        updateChain.doUpdate(loginSuccessData, historyData);
    }
}
